﻿using UnityEngine;
using System.Collections;

public class SpawnRule : MonoBehaviour {

	public GameObject monsterPrefab;//怪物预制体
	public float initTime;//关卡多久开始生成怪物
	public float period;//多久生成一次怪
	public int maxNum;//生成最大数量
	public int hp;//血量
	public int attack;//伤害值

	float levelSinceStartTime=0;//关卡开始经过的时间
	float startLevelTime=0;//关卡开始的时间
	float periodTimer=0;//周期计时器
	int num=0;//当前生成数量

    private Player player;

	void Start()
	{
        player = FindObjectOfType<Player>();
		startLevelTime = Time.time;
	}

    //怪物生成规则
	void Update () {
		if (player.isDeath)
			return;
		levelSinceStartTime = Time.time - startLevelTime;
		if (num >= maxNum)
			return;
		if (levelSinceStartTime > initTime)
		{
			periodTimer+=Time.deltaTime;
			if(periodTimer>period)
			{
				Enemy e=(Enemy) UnitManager.Instance.CreateEnemy(monsterPrefab);
				e.maxHp=hp;
				e.power=attack;
				num++;
				periodTimer=0;
			}
		}
	}
}
